﻿<template>
     <el-dialog :close-on-click-modal="false" :close-on-press-escape="false" title="编辑" :append-to-body="true" :visible="modalEdit" @close="modalEdit=false" @opened="opened" @closed="closed">
        <el-form ref="sysBranch" label-position="left" label-width="120px" :model="sysBranch" :rules="rules">
            <input type="hidden" :model="sysBranch.id" />
            <el-form-item label="所属上级" prop="parentId" v-if="sysBranch.id!=1">
                <el-select size="small" v-model="sysBranch.parentId" placeholder="所属上级" style="width: 240px">
                    <el-option v-for="user in branchs" :key="user.id" :label="user.name" :value="user.id">
                    </el-option>
                </el-select>
                <el-button size="small" :loading="branchLoading" @click="getBranchs">获取</el-button>
            </el-form-item>
            <el-form-item label="部门/公司名称" prop="name" v-if="sysBranch.id!=1">
                <el-input size="small" v-model="sysBranch.name" placeholder="部门/公司名称" style="width: 240px" />
            </el-form-item>
            <el-form-item label="状态" prop="state" v-if="sysBranch.id!=1">
                <el-radio-group size="small" v-model="sysBranch.state">
                    <el-radio :label="1">启用</el-radio>
                    <el-radio :label="0">禁用</el-radio>
                </el-radio-group>
            </el-form-item>
            <el-form-item size="small" label="类型" prop="type" v-if="sysBranch.id!=1">
                <el-radio-group v-model="sysBranch.type">
                    <el-radio :label="1">子公司</el-radio>
                    <el-radio :label="2">内部部门</el-radio>
                </el-radio-group>
            </el-form-item>
            <el-form-item size="small" label="联系人" prop="linkMan">
                <el-input v-model="sysBranch.linkMan" placeholder="联系人" style="width: 240px" />
            </el-form-item>
            <el-form-item size="small" label="联系电话" prop="phone">
                <el-input v-model="sysBranch.phone" placeholder="联系电话" style="width: 240px" />
            </el-form-item>
        </el-form>
        <div slot="footer">
            <el-button @click="cancel">取消</el-button>
            <el-button type="primary" :loading="modalLoading" @click="ok">确定</el-button>
        </div>
    </el-dialog>
    </div>
</template>
<script>
export default {
    name: 'SysBranch',
    data() {
        return {
            branchLoading: false,
            modalEdit: false,
            modalLoading: false,
            sysBranchId: "",
            sysBranch: {
                id: null,
                parentId:null,
                state: 1,
                type: 2
            },
            branchs: [],
            rules: {
                parentId: [{ required: true, message: "上级必选" }],
                name: [{ required: true, message: "名称必填" }],

                type: [{ required: true, message: "类型不能为空" }],
                state: [{ required: true, message: "状态不能为空" }],

            }
        };
    },
    mounted: function() {


    },
    methods: {
        getBranchs: function() {
            this.branchLoading = true;
            this.$http.post("/sys/sysdepartment/search.action", {}).then(response => {

                var body = response;
                if (body.status == 0) {
                    this.branchs = body.obj;
                } else {
                    this.errorMessage(body.info);
                }
                this.branchLoading = false;
            }, response => { this.branchLoading = false; });
        },
        opened() {
            this.getBranchs();
            this.getDetail();

        },
        closed: function() {
            this.sysBranch = {
                id: null,
                parentId:null,
                state: 1,
                type: 2
            }
        },
        showDialog(id, parentId) {

            this.sysBranchId = id;
            if (!id) {
                this.sysBranch.id = null;
            }
            if (parentId) {
                this.sysBranch.parentId = parentId;
            }
           
            this.modalEdit = true;
        },
        getDetail: function() {
            if (this.sysBranchId == null || this.sysBranchId == undefined || this.sysBranchId == "") {
                return;
            }
            var me = this;
            me.showLoading();
            me.$http.post("/sys/sysdepartment/show_" + me.sysBranchId + ".action").then(response => {
                var body = response;
                if (body.status == 0) {
                    me.sysBranch = body.obj;

                }
                me.hideLoading();
            }, response => {
                me.hideLoading();
            });
        },
        ok: function() {
            var ok = false;
            this.$refs['sysBranch'].validate((valid) => {
                ok = valid;
                if (valid) {

                } else {
                    this.errorMessage('请认真填写数据');
                }
            })
            if (!ok) return;
            var me = this;
            me.modalLoading = true;
            me.$http.post("/sys/sysdepartment/doSave.action", me.sysBranch).then(response => {
                var body = response;
                if (body.status == 0) {
                    me.successMessage("保存成功！");
                    me.modalEdit = false;
                    me.$emit("ok");

                } else {
                    me.$errorMessage(body.info);
                }
                me.modalLoading = false;
            }, response => {
                me.modalLoading = false;
            });
        },
        cancel: function() {

            this.modalEdit = false;
            // this.$emit("ok");
        }
    }
}
</script>
<style scoped>
</style>